//
//  HMStatusRetweetView.swift
//  Weibo
//
//  Created by 黄亦寒 on 2016/9/24.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit
import YYText
//转发微博
class HMStatusRetweetView: UIView {

    //记录转发微博底部约束
    var retweetViewBottomConstraint:Constraint?
    
    var statusViewModel:HMStatusViewModel? {
        didSet {
            //contentLabel.text = statusViewModel?.retweetContent
            //设置转发微博的富文本
            contentLabel.attributedText = statusViewModel!.retweetAtrributedString
            //去除转发微博底部约束，再根据有无配图更新约束
            retweetViewBottomConstraint?.uninstall()
            if let picUrls = statusViewModel?.status.retweeted_status?.pic_urls,picUrls.count > 0 {
                //代表有配图，配图不隐藏，更新转发微博的底部约束为配图的底部约束加间距,然后绑定数据
                pictureView.isHidden = false
                self.snp_updateConstraints(closure: { (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(10).constraint
                })
                
                pictureView.picUrls = picUrls
            }else{
                //代表没有配图，配图隐藏，更新转发微博的底部约束为contentLabel的底部约束加间距
                pictureView.isHidden = true
                self.snp_updateConstraints(closure: { (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(10).constraint
                })
            }
        }
    }
    
    //文字内容
    private lazy var contentLabel:YYLabel = {
//        let label = UILabel(textColor:UIColor.darkGray,fontSize:14)

        let label = YYLabel()
        label.preferredMaxLayoutWidth = ScreenWidth - 20
        label.numberOfLines = 0
        return label
    }()

    //配图
    private lazy var pictureView:HMStatusPictureView = {
        let view = HMStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        backgroundColor = UIColor(white:0.95,alpha:1)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(10)
            make.left.equalTo(self).offset(10)
            make.right.equalTo(self).offset(-10)
    
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
        }
        
        //转发微博的底部等于contentLabel的底部加上10的间距
        self.snp_makeConstraints { (make) in
            retweetViewBottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(10).constraint
        }
        
        //设置点击高亮文字执行的闭包
        contentLabel.highlightTapAction = { (containerView,attributedString,range,rect) in
            //根据匹配的范围获取对应的字符串
            let text = (attributedString.string as NSString).substring(with: range)
            if text.hasPrefix("http") {
                print("点击链接")
            }
            print(text)
            
            
        }
    }
}
